package cn.wolfcode.shop.member.web.advice;

import cn.wolfcode.cloud.exception.BusinessException;
import cn.wolfcode.cloud.result.CodeMsg;
import cn.wolfcode.cloud.result.Result;
import cn.wolfcode.shop.member.result.MemberServerCodeMsg;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MemberServerExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result handlerBusinessException(BusinessException businessException){
        CodeMsg codeMsg = businessException.getCodeMsg();
        Result result  = Result.error(codeMsg);
        return result;
    }

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result handlerBusinessException(BindException bindException){ //参数校验错误
        String msg=bindException.getAllErrors().get(0).getDefaultMessage();
        MemberServerCodeMsg paramCodeMsg = MemberServerCodeMsg.PARAM_CODE_MSG.fillArgs(msg);
        return Result.error(paramCodeMsg);
    }
    /*@ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handlerException(Exception exception){
        System.out.println(exception.getClass());
        return Result.success();
    }*/
}
